#ifndef AACENCODER_H
#define AACENCODER_H

#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavutil/opt.h>
#ifdef __cplusplus
}
#endif

#include "mediabase.h"
#include "codecs.h"
#include "easylogging++.h"

class AACEncoder
{
public:
    AACEncoder();

    RET_CODE Init(const Properties& properties, AVRational tb);
    virtual ~AACEncoder();

    virtual RET_CODE Input(const AVFrame* frame);
    virtual RET_CODE Output(AVPacket* pkt);
    virtual uint32_t GetRate() {return ctx_->sample_rate?ctx_->sample_rate:8000;}
    virtual int get_sample_rate() {return ctx_->sample_rate;}
    virtual int64_t get_bit_rate() {return ctx_->bit_rate;}
    virtual uint64_t get_channel_layout() {return ctx_->channel_layout;}
    virtual AVRational get_time_base() {return ctx_->time_base;}
    // 每通道需要的样本数
    virtual uint32_t get_frame_size()
    {
        return ctx_->frame_size;
    }
    virtual uint32_t get_sample_fmt()
    {
        return ctx_->sample_fmt;
    }
    // 一帧需要的样本字节数
    virtual uint32_t GetFrameByteSize()
    {
        return frame_byte_size_;
    }
    virtual int get_profile()
    {
        return ctx_->profile;
    }
    virtual int get_channels()
    {
        return ctx_->channels;
    }
    virtual int get_sample_format()
    {
        return ctx_->sample_fmt;
    }

    void GetAdtsHeader(uint8_t *adts_header, int aac_length)
    {
        uint8_t freqIdx = 0;    //0: 96000 Hz  3: 48000 Hz 4: 44100 Hz
        switch (ctx_->sample_rate)
        {
        case 96000: freqIdx = 0; break;
        case 88200: freqIdx = 1; break;
        case 64000: freqIdx = 2; break;
        case 48000: freqIdx = 3; break;
        case 44100: freqIdx = 4; break;
        case 32000: freqIdx = 5; break;
        case 24000: freqIdx = 6; break;
        case 22050: freqIdx = 7; break;
        case 16000: freqIdx = 8; break;
        case 12000: freqIdx = 9; break;
        case 11025: freqIdx = 10; break;
        case 8000: freqIdx = 11; break;
        case 7350: freqIdx = 12; break;
        default:
            LOG(ERROR) << "can't support sample_rate:" << ctx_->sample_rate;
            freqIdx = 4;
            break;
        }
        uint8_t ch_cfg = ctx_->channels;
        uint32_t frame_length = aac_length + 7;
        adts_header[0] = 0xFF;
        adts_header[1] = 0xF1;
        adts_header[2] = ((ctx_->profile) << 6) + (freqIdx << 2) + (ch_cfg >> 2);
        adts_header[3] = (((ch_cfg & 3) << 6) + (frame_length  >> 11));
        adts_header[4] = ((frame_length & 0x7FF) >> 3);
        adts_header[5] = (((frame_length & 7) << 5) + 0x1F);
        adts_header[6] = 0xFC;
    }
    AVCodecContext* GetCodecContext() {
        return ctx_;
    }
private:
    int sample_rate_;
    int channels_;
    int bitrate_;
    int channel_layout_;

    AVCodec* codec_ = nullptr;
    AVCodecContext* ctx_ = nullptr;
    AVFrame* frame_ = nullptr;
    AudioCodec::Type type_;
    int frame_byte_size_;
};

#endif
















































